/*
 * 企业列表
 * @Author: along
 * @Date: 2019-12-09 10:24:00
 */
<template>
    <div class="container companyList">
        <!-- header -->
        <div class="header">
            <div class="header-box">
                <p class="header-box-title">公司名称:</p>
                <el-select
                    v-model="header.company"
                    filterable
                    placeholder="请选择"
                    size="small"
                    clearable
                    style="width:240px"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in tableData"
                        :key="item.hire_no"
                        :label="item.company_name"
                        :value="item.hire_no"
                    />
                </el-select>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="companyList"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="company_name"
                label="公司名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="company_logo"
                min-width="110"
                label="公司Logo"
                align="center"
            >
                <template slot-scope="scope">
                    <img
                        :src="scope.row.company_logo !== '' ? scope.row.company_logo : 'http://pic.wxhand.com/wei_image/f34d34f8c2f2ca5b5d732cdb0be9c9d4.png' "
                        class="scope-price"
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="kf_count"
                label="客服数量"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span
                        class="cursor-font"
                        @click="fnJump(scope.row)"
                    >{{ scope.row.kf_count }}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="fnLookvideo(scope.row)"
                    >
                        查看视频({{ scope.row.course_count }})
                    </el-button>
                    <el-button
                        type="text"
                        @click="fnBindKf(scope.row)"
                    >
                        绑定客服
                    </el-button>
                    <el-button
                        type="text"
                        @click="fnUnBindkf(scope.row)"
                    >
                        解绑客服
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 绑定客服 -->
        <el-dialog
            title="绑定客服"
            :visible.sync="dialogVisibleBind"
            width="580px"
            :close-on-click-modal="false"
            center
            class="dialogVisibleBind"
        >
            <el-form
                :model="ruleFormBind"
                label-width="100px"
                class="demo-ruleForm"
                @submit.native.prevent
            >
                <el-form-item
                    label="手机号:"
                >
                    <el-input
                        v-model="ruleFormBind.phone"
                        size="small"
                        placeholder="请输入手机号"
                        @keyup.native.enter="fnBindSearch()"
                        :style="{'width': '350px'}"
                    />
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnBindSearch()"
                    >
                        搜索
                    </el-button>
                </el-form-item>
                <el-table
                    :data="tableDataBind"
                    height="100%"
                    style="width: 100%;"
                    border
                    v-if="tableDataBind.length > 0"
                >
                    <el-table-column
                        label="用户姓名"
                        prop="true_name"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="电话号码"
                        prop="phone"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="学校名称"
                        prop="school_name"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="操作"
                        prop="school_name"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <span
                                @click="fnDeleteData(scope.$index)"
                                class="cursor-font"
                            >删除</span>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialogVisibleBind = false"
                    size="small"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="fnConirmBind()">绑 定</el-button>
            </span>
        </el-dialog>
        <!-- /绑定客服 -->

        <!-- 解绑客服 -->
        <el-dialog
            title="解绑客服"
            :visible.sync="dialogVisibleUnBind"
            width="520px"
            :close-on-click-modal="false"
            center
            class="dialogVisibleUnBind"
        >
            <el-checkbox-group
                v-model="checkListKf"
            >
                <div
                    v-for="(item,index) in kfList"
                    :key="index"
                >
                    <el-checkbox
                        :label="item.student_no"
                        class="el-checkList"
                        style="display:flex;margin-bottom:15px;"
                    >
                        <p class="dialogVisibleUnBind_text">
                            {{ item.true_name !== '' ? item.true_name + ' -' : '' }}
                            {{ item.school_name !== '' ? item.school_name + ' -' : '' }}
                            {{ item.student_no }}
                        </p>
                    </el-checkbox>
                </div>
            </el-checkbox-group>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    @click="dialogVisibleUnBind = false">取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    :disabled="ifBind"
                    @click="fnUntying()">解 绑</el-button>
            </span>
        </el-dialog>

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
export default {
    /**
     * @param  {Object}    authResult           //表单权限
     * @param  {Array}     tableData            //表单数据
     * @param  {Object}    page                 //表单分页
     * @param  {Object}    header               //表单检索字段
     * @param  {Array}     companyList          //表单检索字段
     * @param  {String}    pathName             //上一页路由名称
     * @param  {Array}     tableDataBind        //绑定客服数据列表
     * @param  {Boolean}   dialogVisibleBind    //绑定客服开关
     * @param  {Boolean}   dialogVisibleUnBind  //解绑客服开关
     * @param  {Array}     kfList               //绑定客服数据
     * @param  {String}    checkListKf          //解绑客服选择数据
     * @param  {Boolean}   ifBind               //解绑状态
     * @param  {String}    hire_no              //公司编号
     */
    data () {
        return {
            student_no: '',
            hire_no: '',
            ifBind: true,
            checkListKf: [],
            kfList: [],
            dialogVisibleUnBind: false,
            ruleFormBind: {
                phone: ''
            },
            dialogVisibleBind: false,
            tableDataBind: [],
            pathName: '',
            companyList: [],
            header: {
                company: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    watch: {
        dialogVisibleBind (val) {
            if (!val) {
                this.tableDataBind = [];
            }
        },
        dialogVisibleUnBind (val) {
            if (!val) {
                this.kfList = [];
                this.checkListKf = [];
            }
        },
        checkListKf (val) {
            if (val.length > 0) {
                this.ifBind = false;
            } else {
                this.ifBind = true;
            }
        }
    },
    beforeRouteEnter(to, from, next) {
        next(vm => {
            vm.pathName = from.name;
        });
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        // this.getCompanylist(1);
        this.fnParamUrl();
        this.fnBreadcrumb();
        this.fnRecord();
        this.getTableData();
    },
    destroyed(){
        window.removeEventListener('popstate', this.fnGoBack, false);
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '工作'},{title: "企业列表"}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/company%5Cget_list', {
                page_id: page || this.page.now_page,
                hire_no: this.header.company,
                student_no: this.student_no
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 获取公司列表
         */
        getCompanylist (page) {
            this.$post('/student/company%5Cget_list',{
                page_id: page
            },resp=>{
                if (resp.code == 1) {
                    this.companyList = this.companyList.concat(resp.data.list);
                    if (resp.data.list.length < 10) {
                        return;
                    } else {
                        this.getCompanylist(page + 1);
                    }
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 获取路由参数
         */
        fnParamUrl () {
            let query = this.$route.query;

            if (query.page !== undefined) {
                this.page.now_page = Number(query.page);
            }

            if (query.student_no !== undefined) {
                this.student_no = query.student_no;
            }
        },

        /**
         * 记录上一页路由,监听页面返回并重定向
         */
        fnRecord () {
            if (window.history && window.history.pushState) {
                history.pushState(null, null, document.URL);
                window.addEventListener('popstate', this.fnGoBack, false);
            }
        },

        /**
         * 上一页重定向
         */
        fnGoBack  () {
            this.$router.replace({ name: this.pathName });
        },

        /**
         * 绑定/解绑_客服
         * @param   {Object}    row     //每行表单参数
         */
        fnBindKf (row) {
            this.hire_no = row.hire_no;
            this.dialogVisibleBind = !this.dialogVisibleBind;
        },

        /**
         * 搜索客服
         */
        fnBindSearch () {
            if (this.ruleFormBind.phone == '') {
                this.$message({
                    type: 'waring',
                    message: '手机号不可以为空',
                    center: true
                });
                return;
            }

            this.$post('/student/student_user%5Cget_list_suggest',{
                keyword: this.ruleFormBind.phone
            },resp=>{
                if (resp.code == 1) {
                    if (resp.data.list.length == 0) {
                        this.$message({
                            type: 'waring',
                            message: '未搜索到该用户!',
                            center: true
                        });
                        this.ruleFormBind.phone = '';
                        return;
                    }
                    this.tableDataBind = resp.data.list;
                    this.ruleFormBind.phone = '';
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 删除客服
         */
        fnDeleteData (index) {
            this.tableDataBind.splice(index,1);
        },

        /**
         * 绑定客服
         */
        fnConirmBind () {
            if (this.tableDataBind.length == 0) {
                this.$message({
                    type: 'waring',
                    message: '绑定客服不可以为空',
                    center: true
                });
                return;
            }

            let kf_student = '';

            this.tableDataBind.map(item=>{
                kf_student += item.student_no + ',';
            });
            this.$post('/student/hire_kf%5Cbind_kf',{
                hire_no: this.hire_no,
                student_nos: kf_student
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData(this.page.now_page);
                    this.dialogVisibleBind = !this.dialogVisibleBind;
                    this.$message({
                        type: 'success',
                        message: '绑定成功',
                        center: true
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 解绑客服
         */
        fnUnBindkf (row) {
            this.hire_no = row.hire_no;
            this.dialogVisibleUnBind = !this.dialogVisibleUnBind;
            this.$post('/student/hire_kf%5Cget_bind_kf_list',{
                hire_no: row.hire_no
            },resp=> {
                if (resp.code == 1) {
                    if (resp.data.length !== 0) {
                        this.kfList = this.kfList.concat(resp.data.list);
                    }
                }
            });
        },

        /**
         * 确定解绑客服
         */
        fnUntying (row) {
            this.$post('/student/hire_kf%5Cun_bind_kf',{
                hire_no: this.hire_no,
                student_nos: this.checkListKf.join()
            },resp=>{
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                    this.dialogVisibleUnBind = !this.dialogVisibleUnBind;
                    this.getTableData(this.page.now_page);
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 查看视频
         * @param   {Object}    row     //每行表单参数
         */
        fnLookvideo (row) {
            this.$router.replace({
                name: 'companyVideo',
                query: {
                    page: this.page.now_page,
                    hire_no: row.hire_no
                }
            });
        },

        /**
         * 跳转客服管理
         */
        fnJump (row) {
            this.$router.replace({
                name: 'workService',
                query: {
                    hire_no: row.hire_no
                }
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    .cursor-font {
        color: rgb(64, 158, 255);
        cursor: pointer;
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .check_list {
        display: flex;
        height: 18px;
        align-items: center;
    }
</style>
<style lang="less">
    .companyList .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .companyList .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .companyList .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .companyList .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
    .companyList .dialogVisibleBind .el-table__body-wrapper{
        height: auto !important;
    }
    .companyList .dialogVisibleBind .el-form .el-table {
        max-height: 400px;
        overflow-y: auto;
        &::-webkit-scrollbar{
            width: 2px;
        }
    }
    .companyList .dialogVisibleUnBind .el-checkbox-group {
        max-height: 400px;
        overflow-y: auto;
        &::-webkit-scrollbar{
            width: 2px;
        }
        .dialogVisibleUnBind_text {
            width: 430px;
            display: inline-block;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            margin-top: -2px;
        }
    }
</style>